package com.loveyukari.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.loveyukari.dao.AuthFacilityDao;
import com.loveyukari.model.AuthFacility;

/**
*
*yangshaohua
*2017年6月9日下午5:16:10
**/
@Repository
public class JdbcAuthFacilityDao extends BaseDao implements AuthFacilityDao{

	private class AuthFacilityRowMapper implements RowMapper<AuthFacility>{
		
		public AuthFacility mapRow(ResultSet rs, int rowNum) throws SQLException {
			AuthFacility authFacility = new AuthFacility();
			authFacility.setAuthId(rs.getInt("auth_id"));
			authFacility.setAuthTalk(rs.getBoolean("auth_talk"));
			authFacility.setLiveAlert(rs.getBoolean("live_alert"));
			authFacility.setLiveSign(rs.getBoolean("live_sign"));
			authFacility.setYukariTalk(rs.getBoolean("yukari_talk"));
			return authFacility;
		}
		
	}
	

	
	public void updateAuthFacility(AuthFacility authFacility) {
		String sql = "update auth_facility set live_alert = ?,yukari_talk = ?,live_sign = ?,auth_talk = ? where auth_id = ?";
		List<Object> params = new ArrayList<Object>();
		params.add(authFacility.isLiveAlert());
		params.add(authFacility.isYukariTalk());
		params.add(authFacility.isLiveSign());
		params.add(authFacility.isAuthTalk());
		params.add(authFacility.getAuthId());
		this.getJdbcTemplate().update(sql,params.toArray());
		
		
	}

	public void insertAuthFacility(AuthFacility authFacility) {
		String sql = "insert into auth_facility(live_alert,yukari_talk,live_sign,auth_talk,auth_id) values(?,?,?,?,?)";
		List<Object> params = new ArrayList<Object>();
		params.add(authFacility.isLiveAlert());
		params.add(authFacility.isYukariTalk());
		params.add(authFacility.isLiveSign());
		params.add(authFacility.isAuthTalk());
		params.add(authFacility.getAuthId());
		this.getJdbcTemplate().update(sql,params.toArray());
	}
	
	
	public AuthFacility selectAuthFacility(int id){
		String sql = "select * from auth_facility where auth_id = ?";
		List<AuthFacility> list = this.getJdbcTemplate().query(sql, new AuthFacilityRowMapper(),id);
		if (!list.isEmpty()){
			return list.get(0);
		} else {
			return null;
		}
	}
	

}
